package lcr;

import java.util.Arrays;

public class Test0017 {
    public static void main(String[] args) {
        new Solution0017_1().minWindow("ADOBECODEBANC","ABC");
    }
}

class Solution0017_1 {
    public String minWindow(String s, String t) {
        char[] charArray1 = s.toCharArray();
        char[] charArray2 = t.toCharArray();

        int[] reocrd = new int[59];
        Arrays.fill(reocrd,Integer.MIN_VALUE/2);
        int length1 = charArray1.length,length2 = charArray2.length;
        for (int i = 0; i < length2; i++) {
            int index = charArray2[i] - 'A';

            if (reocrd[index]<0) reocrd[index]=1;
            else reocrd[index]++;
        }

        int l=0,r=0,l1=0,r1=Integer.MAX_VALUE,limit=length2;
        for (;r<length1;r++){
            int index = charArray1[r] - 'A';
            if (reocrd[index]>0){
                limit--;
            }
            reocrd[index]--;

            for (;l<r;l++){
                int index1 = charArray1[l] - 'A';
                if (reocrd[index1] >= 0){
                    break;
                } else {
                    reocrd[index1]++;
                }
            }

            if (limit==0&&r+1-l<r1-l1) {
                l1=l;
                r1=r+1;
            }
        }
        if (limit==0) return s.substring(l1,r1);
        return "";
    }
}
